﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ImageProcessing
{
	public enum OperationType {Affine, Enhancement};
	public enum AffineType { Rotate, Shear, Scale };

	public partial class formMain : Form
	{
		Bitmap bmIn;
		Bitmap bmOrig;
		Bitmap bmDummy;
		bool isReady;
		bool isChanged;
		double time;
		FilterTypes _filtertypes;

		const int DEFAULTWIDTH = 500;
		const int DEFAULTHEIGHT = 350;


		public formMain()
		{
			bmDummy = new Bitmap(1, 1);

			InitializeComponent();
			toolStripStatusLabel1.Text = "No image";
		}


		//
		//file menu events
		//

		private void openMenuItem_Click(object sender, EventArgs e)
		{
			openFileDialog.Filter = "All Image Files|*.png;*.jpg;*.bmp|Jpeg (*.jpg)|*.jpg|Bitmap Files (*.bmp)|*.bmp|PNG files (*.png)|*.png|TIF files (*.png)|*.tif";
	
			if (openFileDialog.ShowDialog() == DialogResult.OK)
			{
				bmIn = new Bitmap(openFileDialog.FileName);
				bmOrig = new Bitmap(bmIn);
				toolStripStatusLabel1.Text = openFileDialog.FileName.Substring(openFileDialog.FileName.LastIndexOf(@"\") + 1);
				isReady = true;
				picboxMain.Invalidate();
			}
		}

		private void exitMenuItem_Click(object sender, EventArgs e)
		{
			Close();
		}

		//
		//paint events
		//
		private void picboxMain_Paint(object sender, PaintEventArgs e)
		{
			if (isReady)
			{
				picboxMain.BackgroundImage = bmIn;
				picboxMain.Size = bmIn.Size;

				if (bmIn.Width < 900)
					this.Width = bmIn.Width + 50;
				else
					this.Width = 1000;
				if (bmIn.Height < 700)
					this.Height = bmIn.Height + 100;
				else
					this.Height = 800;
			}
			else
			{
				picboxMain.BackgroundImage = bmDummy;
			}
		}

		//
		//Affine Menu Clicks
		//

		private void rotateMenuItem_Click(object sender, EventArgs e)
		{
			ShowEditDialog(OperationType.Affine);
		}

		private void shearMenuItem_Click(object sender, EventArgs e)
		{
			ShowEditDialog(OperationType.Affine);
		}

		private void zoomMenuItem_Click(object sender, EventArgs e)
		{
			ShowEditDialog(OperationType.Affine);
		}


		//
		//helpers
		//
		private bool ShowEditDialog(OperationType optype)
		{
			if (!isReady) return false;
			AffineDialog eDlg = new AffineDialog(bmIn, optype);
			if (eDlg.ShowDialog() == DialogResult.OK)
			{
				//success
				bmIn = new Bitmap(eDlg.ImageOut);
				isChanged = true;
				picboxMain.Invalidate();
				return true;
			}
			return false;

		}

		private void correctionsMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
		{
			if (!isReady) return;

			ColorEnhanceDialog eDlg = new ColorEnhanceDialog(bmIn);
			if (eDlg.ShowDialog() == DialogResult.OK)
			{
				bmIn = new Bitmap(eDlg.ImageOut);
				isChanged = true;
				picboxMain.Invalidate();
			} //end if
		}

		//private void testToolStripMenuItem_Click(object sender, EventArgs e)
		//{
		//    if (!isReady) return;

		//    DateTime begin = DateTime.Now;

		//    double[][][] adYIQ = ColorConversions.ImageToYIQ(bmIn);
		//    bmIn = OrderStatFilters.Median(adYIQ, bmIn.Height, bmIn.Width, 3, bmIn.PixelFormat);

		//    TimeSpan dt = (TimeSpan)(DateTime.Now - begin);
		//    time = dt.TotalMilliseconds;
		//    toolStripStatusLabel1.Text = time.ToString();
		//    Invalidate();
		//    //bmIn = OrderStatFilters.Median(bmIn, 3);
		//}

		//private void medianMenuItem_Click(object sender, EventArgs e)
		//{
		//    if (!isReady) return;

		//    DateTime begin = DateTime.Now;

		//    double[][][] adYIQ = ColorConversions.ImageToYIQ(bmIn);
		//    bmIn = OrderStatFilters.Median(adYIQ, bmIn.Height, bmIn.Width, 3, bmIn.PixelFormat);

		//    TimeSpan dt = (TimeSpan)(DateTime.Now - begin);
		//    time = dt.TotalMilliseconds;
		//    toolStripStatusLabel1.Text = time.ToString();
		//    Invalidate();
		//}

		//private void averageArithmeticMenuItem_Click(object sender, EventArgs e)
		//{
		//    if (!isReady) return;

		//    DateTime begin = DateTime.Now;

		//    double[][][] adYIQ = ColorConversions.ImageToYIQ(bmIn);
		//    bmIn = OrderStatFilters.ArithmeticMean(adYIQ, bmIn.Height, bmIn.Width, 3, bmIn.PixelFormat);

		//    TimeSpan dt = (TimeSpan)(DateTime.Now - begin);
		//    time = dt.TotalMilliseconds;
		//    toolStripStatusLabel1.Text = time.ToString();
		//    Invalidate();
		//}

		//private void geometricAverageMenuItem_Click(object sender, EventArgs e)
		//{
		//    if (!isReady) return;

		//    DateTime begin = DateTime.Now;

		//    double[][][] adYIQ = ColorConversions.ImageToYIQ(bmIn);
		//    bmIn = OrderStatFilters.GeometricMean(adYIQ, bmIn.Height, bmIn.Width, 3, bmIn.PixelFormat);

		//    TimeSpan dt = (TimeSpan)(DateTime.Now - begin);
		//    time = dt.TotalMilliseconds;
		//    toolStripStatusLabel1.Text = time.ToString();
		//    Invalidate();
		//}

		//private void maxToolStripMenuItem_Click(object sender, EventArgs e)
		//{
		//    if (!isReady) return;

		//    DateTime begin = DateTime.Now;

		//    double[][][] adYIQ = ColorConversions.ImageToYIQ(bmIn);
		//    bmIn = OrderStatFilters.Max(adYIQ, bmIn.Height, bmIn.Width, 3, bmIn.PixelFormat);

		//    TimeSpan dt = (TimeSpan)(DateTime.Now - begin);
		//    time = dt.TotalMilliseconds;
		//    toolStripStatusLabel1.Text = time.ToString();
		//    Invalidate();
		//}

		//private void minToolStripMenuItem_Click(object sender, EventArgs e)
		//{
		//    if (!isReady) return;

		//    DateTime begin = DateTime.Now;

		//    double[][][] adYIQ = ColorConversions.ImageToYIQ(bmIn);
		//    bmIn = OrderStatFilters.Min(adYIQ, bmIn.Height, bmIn.Width, 3, bmIn.PixelFormat);

		//    TimeSpan dt = (TimeSpan)(DateTime.Now - begin);
		//    time = dt.TotalMilliseconds;
		//    toolStripStatusLabel1.Text = time.ToString();
		//    Invalidate();
		//}

		//private void mToolStripMenuItem_Click(object sender, EventArgs e)
		//{
		//    if (!isReady) return;

		//    DateTime begin = DateTime.Now;

		//    double[][][] adYIQ = ColorConversions.ImageToYIQ(bmIn);
		//    bmIn = OrderStatFilters.MidPoint(adYIQ, bmIn.Height, bmIn.Width, 3, bmIn.PixelFormat);

		//    TimeSpan dt = (TimeSpan)(DateTime.Now - begin);
		//    time = dt.TotalMilliseconds;
		//    toolStripStatusLabel1.Text = time.ToString();
		//    Invalidate();
		//}

		//private void alphaTrimMeanToolStripMenuItem_Click(object sender, EventArgs e)
		//{
		//    if (!isReady) return;

		//    DateTime begin = DateTime.Now;

		//    double[][][] adYIQ = ColorConversions.ImageToYIQ(bmIn);
		//    bmIn = OrderStatFilters.AlphaTrimMean(adYIQ, bmIn.Height, bmIn.Width, 3, bmIn.PixelFormat,4);

		//    TimeSpan dt = (TimeSpan)(DateTime.Now - begin);
		//    time = dt.TotalMilliseconds;
		//    toolStripStatusLabel1.Text = time.ToString();
		//    Invalidate();
		//}

		//private void contraharmonicMenuItem_Click(object sender, EventArgs e)
		//{
		//    if (!isReady) return;

		//    DateTime begin = DateTime.Now;

		//    double[][][] adYIQ = ColorConversions.ImageToYIQ(bmIn);

		//    TimeSpan dt = (TimeSpan)(DateTime.Now - begin);
		//    bmIn = OrderStatFilters.Contraharmonic(adYIQ, bmIn.Height, bmIn.Width, 3, bmIn.PixelFormat, 0);


		//    time = dt.TotalMilliseconds;
		//    toolStripStatusLabel1.Text = time.ToString();
		//    Invalidate();
		//}

		private void filtersMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
		{
			if (!isReady) return;

			switch (e.ClickedItem.Name)
			{
				case ("medianMenuItem"):
					_filtertypes = FilterTypes.Median;
					break;
				case ("arithmeticmeanMenuItem"):
					_filtertypes = FilterTypes.ArithmeticMean;
					break;
				case ("geometricmeanMenuItem"):
					_filtertypes = FilterTypes.GeometricMean;
					break;
				case ("minMenuItem"):
					_filtertypes = FilterTypes.Min;
					break;
				case ("maxMenuItem"):
					_filtertypes = FilterTypes.Min;
					break;
				case ("contraharmonicMenuItem"):
					_filtertypes = FilterTypes.Contraharmonic;
					break;
				case ("alphatrimmeanMenuItem"):
					_filtertypes = FilterTypes.AlphaMean;
					break;
				case ("midpointMenuItem"):
					_filtertypes = FilterTypes.Midpoint;
					break;
				case ("sobelMenuItem"):
					_filtertypes = FilterTypes.Sobel;
					break;
				case ("prewittMenuItem"):
					_filtertypes = FilterTypes.Prewitt;
					break;
				default:
					break;
			} //end switch

			FilterDialog eDlg = new FilterDialog(bmIn,_filtertypes);
			if (eDlg.ShowDialog() == DialogResult.OK)
			{
				bmIn = new Bitmap(eDlg.ImageOut);
				isChanged = true;
				picboxMain.Invalidate();
			} //end if
		}

		private void specialMenuItem_Click(object sender, EventArgs e)
		{
			SpecialEffectsDialog eDlg = new SpecialEffectsDialog();
			if (eDlg.ShowDialog() == DialogResult.OK)
			{
				isChanged = true;
			}
		}












	}
}
